﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace Snake.Model
{
   
        class Serveur
        {
            private TcpListener tcpListener;
            private TcpClient tcpClient;
            private Thread listenThread;
            private String NameOtherPlayer;
            private byte[] message = new byte[4096];
            private TcpClient client;
            private Boolean ClientConnected;

            public Boolean clientconnected
            {
                get { return this.ClientConnected; }
                set { this.ClientConnected = value; }
            }

            public Serveur()
            {
                this.tcpListener = new TcpListener(IPAddress.Any, 3000);
                //utiliser un thread pour envoyer
                this.listenThread = new Thread(new ThreadStart(ListenForClients));
                this.listenThread.Start();
            
            
            }


            private void ListenForClients()
            {
                this.tcpListener.Start();

                while (true)
                {
                    //blocks until a client has connected to the server
                    TcpClient client = this.tcpListener.AcceptTcpClient();
                    //System.Windows.Forms.MessageBox.Show("Maintenant connexion effectué");
                    //create a thread to handle communication 
                    //with connected client
                    //new ParameterizedThreadStart(
                    Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));

                    clientThread.Start(client);
                    
                }
            }

            private void Envoi(string name)
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream stream = tcpClient.GetStream();

                // Send back a response.
                byte[] msg = System.Text.Encoding.ASCII.GetBytes(name);

                stream.Write(msg, 0, msg.Length);

            }
            private void HandleClientComm(object client)
            {
                TcpClient tcpClient = (TcpClient)client;
                NetworkStream clientStream = tcpClient.GetStream();

               
                int bytesRead;

                while (true)
                {
                    bytesRead = 0;

                    try
                    {
                        //blocks until a client sends a message
                        //System.Windows.Forms.MessageBox.Show("Ici");
                        ClientConnected = false;
                        bytesRead = clientStream.Read(message, 0, 4096);
                        ClientConnected = true;
                        System.Windows.Forms.MessageBox.Show("Client Connecté");                       
                    }
                    catch
                    {
                        //a socket error has occured
                        break;
                    }

                    if (bytesRead == 0)
                    {
                        //the client has disconnected from the server
                        break;
                    }

                    //message has successfully been received
                    ASCIIEncoding encoder = new ASCIIEncoding();
                    System.Windows.Forms.MessageBox.Show("Coté Serveur : Nom de l'autre Joueur" + encoder.GetString(message, 0, bytesRead));
                    NameOtherPlayer = encoder.GetString(message, 0, bytesRead);
                    //System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
                    //réponse 
                    //ne dois pas être fait tant que la connexion n'est pas réaliser ..
                    Envoi("fucker");
                }

                //tcpClient.Close();
            }

            public void EndServ()
            {
                this.listenThread.Abort();
                this.tcpClient.Close();
            }
        }
    
}
